package com.yfbao.horizon.schedule.tk.controller;

import io.lettuce.core.support.ConnectionPoolSupport;
import io.lettuce.core.api.StatefulConnection;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class RedisPoolController {

    @Autowired
    private RedisConnectionFactory redisConnectionFactory;

    @GetMapping("/redis/pool/info")
    public Map<String, Object> getPoolInfo() {
        Map<String, Object> poolInfo = new HashMap<>();

        if (redisConnectionFactory instanceof LettuceConnectionFactory) {
            LettuceConnectionFactory factory = (LettuceConnectionFactory) redisConnectionFactory;
//
//            // 获取原生连接池
//            GenericObjectPool<StatefulConnection<byte[], byte[]>> pool =
//                    ConnectionPoolSupport.extractPool(factory.getConnection());
//
//            info.put("maxTotal", pool.getMaxTotal());
//            info.put("maxIdle", pool.getMaxIdle());
//            info.put("minIdle", pool.getMinIdle());
//            info.put("numActive", pool.getNumActive());
//            info.put("numIdle", pool.getNumIdle());
//            info.put("numWaiters", pool.getNumWaiters());
        }

        return poolInfo;
    }
}